package com.quark.service.system;

import com.quark.mapper.system.SysRoleMapper;
import com.quark.mapper.system.SysRoleMenuMapper;
import com.quark.model.system.role.SysRole;
import com.quark.service.redis.RedisService;
import com.quark.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 张高昌
 * @version 版本
 * @date 2021/11/11 9:38
 * @description: 功能描述
 */
@Service
@RequiredArgsConstructor
public class RoleService {

    private final SysRoleMapper sysRoleMapper;

    private final SysRoleMenuMapper sysRoleMenuMapper;

    private final RedisService redisService;

    public List<SysRole> getPageList() {
//        Long userId = SecurityUtils.getUserId();
        List<SysRole> list = (List<SysRole>) redisService.get("sys_role_all:");
        if (list == null || list.isEmpty()) {
            list = sysRoleMapper.selectListAll();
            redisService.set("sys_role_all:", list, 14400);
        }

        return list;
    }

    public boolean insertRole(SysRole sysRole) {
        sysRole.setState(1);
        sysRole.setCreateTime(new Date());
        int result = sysRoleMapper.insert(sysRole);
        redisService.del("sys_role_all");
        return result > 0;
    }

    public boolean updateRole(SysRole sysRole) {

        sysRole.setUpdateTime(new Date());
        int result = sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        redisService.del("sys_role_all");
        return result > 0;
    }

    public boolean updateRoleState(int id, int state) {
        int result = sysRoleMapper.updateState(id, state);
        redisService.del("sys_role_all");
        return result > 0;
    }

    public boolean deleteRole(int rId) {
        int result = sysRoleMapper.deleteByPrimaryKey(rId);
        redisService.del("sys_role_all");
        return result > 0;
    }

    public List<Integer> getRolePermissions(int rId) {
        List<Integer> list = sysRoleMenuMapper.selectByRidList(rId);
        return list;
    }

}
